package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;
import com.kdtech.utils.HtmlCleaner;

public class _10jqkaNewsAnalyse implements AnalyseNews {

	//http://stock.10jqka.com.cn/20130507/c534467790.shtml
	private static final String[] regex={
		"http://.*.10jqka.com.cn/[0-9]*/c[0-9]*.shtml",
		"http://.*.10jqka.com.cn/[0-9]*/c[0-9]*.shtml\\?ts=[0-9]",
		"http://.*.10jqka.com.cn/.*/c[0-9]+.shtml",
	};

	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		if (urlMeta.getHtml() == null) {
		}
		String url=urlMeta.getUrl();
		if (!isDetailPage(url)) {
		}
		NewsMeta news=new NewsMeta();

		String htmltxt=urlMeta.getHtml();
		Document doc=Jsoup.parse(htmltxt);
		doc.select("#arctTailMark").remove();
		String title=null;
		String content=null;
		Long date=null;
		title=doc.select("#J_article h1").text();
		if (StringUtils.isBlank(title)) {
			title=doc.select("h1").text();
		}
		if (StringUtils.isBlank(title)) {
			title=doc.select("title").text();
			if (title.contains("_")) title=StringUtils.substringBefore(title, "_");
			if (title.contains("-")) title=StringUtils.substringBefore(title, "-");
		}
		content=HtmlCleaner.getContentHtml(url,doc.select(".art_main"));
		if (StringUtils.isBlank(content)) {
			content=HtmlCleaner.getContentHtml(url,doc.select(".pic_desc"));
		}
		if (StringUtils.isBlank(content)) {
			content=HtmlCleaner.getContentHtml(url,doc.select("div#tdcontent"));
		}
		if (StringUtils.isBlank(content)) {
			content=HtmlCleaner.getContentHtml(url,doc.select("div#news_content"));
		}
		if (StringUtils.isBlank(content)) {
			content=HtmlCleaner.getContentHtml(url,doc.select("div.atc-content p"));
		}
		if (date==null ) date=DateUtils.matchDate(doc.select("div.art_info").text());
		if (date==null ) date=DateUtils.matchDate(doc.select(".hdpic_title ").text());
		if (date==null ) date=JSoupUtils.matchDate(doc, "时间：");
		if (date==null ) date=DateUtils.matchDate(doc.select(".time").text());
		news.setUrl(url);
		news.setTitle(title);
		news.setContent(content);
		news.setDate(date);
		String author=null;

		/*
		 * 作者
		 */
		author=JSoupUtils.matchAuthor(doc, "来源：");

		news.setAuthor(author);
		return news;
	}

	public static void main(String[] args) {
		String url="http://stock.10jqka.com.cn/newstock/20150626/c573885114.shtml";
		_10jqkaNewsAnalyse test=new _10jqkaNewsAnalyse();
		url=UrlArgumentTop.FromatUrl(url);
		System.out.println(test.isDetailPage(url));
		System.out.println(DoMainUtils.GetDomainName(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		NewsMeta parserHtml=test.parserHtml(meta);
		System.out.println(parserHtml);
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
